<?php
declare (strict_types = 1);

namespace app\common\libs\phpspreadsheet;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\facade\App;

/**
 * Excel文件管理
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class PhpSpreadSheet 
{
    private static $spreadsheet;
    private static $sheet;
    private static $instance = null;
    private static $path = 'phpExcel';//保存路径
    private static $width = 20;//宽度
    private static $height = 50;//高度
    private static $span;

    public function __clone()
    {
        
    }

    public function __construct()
    {

    }

    /**
     * 单例模式
     *
     * @Author tanruizheng
     * @DateTime 2021-07-27
     * @return void
     */
    public static function instance(){
        if(self::$instance == null){
            self::$instance = new self();
            self::$spreadsheet = new Spreadsheet();
            self::$sheet = self::$spreadsheet->getActiveSheet();
        }
        return self::$instance;
        
    }

    /**
     * 设置标题
     *
     * @Author tanruizheng
     * @DateTime 2021-07-28
     * @param [type] $title
     * @return void
     */
    public function setExcelTitle($title){
        self::$sheet->setCellValue('A1',$title);
        self::$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
        self::$sheet->mergeCells('A1:'.self::$span.'1');
        self::$sheet->getStyle('A1')->getFont()->setName('黑体');
        self::$sheet->getStyle('A1')->getFont()->setSize(20);
        self::$sheet->getStyle('A1')->getFont()->setBold(true);
        return new self;
    }

    /**
     * 设置头部信息
     *
     * @Author tanruizheng
     * @DateTime 2021-07-27
     * @param [type] $header
     * @return void
     */
    public function setExcelHeader($header){
        $span = 'A';
        foreach ($header as $key => $value) {
            self::$sheet->getColumnDimension($span)->setWidth(self::$width);
            self::$sheet->setCellValue($span.'2',$value);
            $span++;
        }
        self::$span = $span;
        self::$sheet->getRowDimension(2)->setRowHeight(self::$height);
        return new self;
    }

    /**
     * execl数据导出
     *
     * @Author tanruizheng
     * @DateTime 2021-07-27
     * @param [type] $data
     * @return void
     */
    public function setExcelContent($data){
        $row = 3;
        foreach ($data as $item) {
            $span = 'A';
            foreach ($item as $val) {
                self::$sheet->setCellValue($span.$row,$val);
                $span++;
            }
            $row++;
        }
        //设置样式
        self::$sheet->getStyle('A1:'.$span.$row)->applyFromArray([
            // 'font' => [
            //     'bold' => true
            // ],
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
                'vertical' => Alignment::VERTICAL_CENTER
            ]
        ]);
        return new self;
    }

    /**
     * Excel表格下载
     *
     * @Author tanruizheng
     * @DateTime 2021-07-27
     * @return void
     */
    public function excelSave($fileName = '', $suffix = 'xlsx', $isSave = false){
        if(empty($fileName)){
            $fileName = date('YmdHis').time();
        }
        if(empty($suffix)){
            $suffix = 'xlsx';//默认格式
        }
        $spreadsheet = self::$spreadsheet;
        $writer = new Xlsx($spreadsheet);
        if($isSave){
            //保存文件
            $savePath = self::savePath().'/'.$fileName.'.'.$suffix;//保存文件路径
            $writer->save($savePath);
            //清空内存
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
        }else{
            //下载文件
            if($suffix == 'xlsx'){
                header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  
            }elseif($suffix == 'xls'){
                header('Content-Type:application/vnd.ms-excel');
            }
            header('Content-Disposition:attachment;filename="' . $fileName .'.' . $suffix .'"');
            header('Cache-Control:max-age=0');
            $writer->save('php://output');
            //清空内存
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
        } 
    }
    
    /**
     * 保存路径
     *
     * @Author tanruizheng
     * @DateTime 2021-07-28
     * @return void
     */
    public static function savePath(){
        $path = App::getRootPath().'/public/'.self::$path;
        if(!is_dir($path)){
            if(mkdir($path,0777) == false){
                return false;
            }
        }
        $dayPath = $path.'/'.date('Ymd');
        if(!is_dir($dayPath)){
            if(mkdir($dayPath,0777) == false){
                return false;
            }
        }
        return $dayPath;
    }
}

